<template>
	<view style="text-align: center;margin-top: 20vh;">
		<image mode="widthFix" :src="path" style="" />
		<ksp-cropper mode="fixed" :width="600" :height="600" :maxWidth="1024" :maxHeight="1024" :url="iconUrl"
			@cancel="oncancel" @ok="onok"></ksp-cropper>
	</view>
</template>

<script>
	import {
		SaveDataOfUserInfo
	} from '@/subPackagesB/pages/interface.js'
	export default {
		data() {
			return {
				iconUrl: "",
				path: ""
			}
		},
		onLoad(options) {
			console.log('获取传递过来的裁剪图片链接', options)
			this.iconUrl = decodeURIComponent(options.path)
			console.log('解析图片地址：', this.iconUrl)
		},
		methods: {
			onok(ev) {
				console.log('点击裁剪确定了', ev)
				this.iconUrl = "";
				this.path = ev.path;
				uni.uploadFile({
					url: this.$ApiUrl + 'Base_Manage/Upload/VipUserImgOssUploadFileByForm',
					filePath: this.path,
					header: {
						'Authorization': 'Bearer ' + uni.getStorageSync('token')
					},
					name: 'file',
				}).then(result => {
					let [err, res] = [...result]
					console.log('获取裁剪后得到的结果', JSON.parse(res.data))
					if (err !== null) return uni.showToast({
						icon: 'none',
						title: '网络连接失败,请重试'
					})
					this.save(JSON.parse(res.data).Url)
				})
			},
			oncancel() {
				// url设置为空，隐藏控件
				this.iconUrl = "";
				uni.navigateBack()
			},
			save(url) {
				let userInfo = uni.getStorageSync('userInfo')
				userInfo.Icon = url
				SaveDataOfUserInfo(userInfo).then(res => {
					if (res.Success) {
						uni.setStorageSync('userInfo', userInfo)
						uni.navigateBack()
					}
				})
			}
		}
	}
</script>